﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace DBHelper.DAL
{
	/// <summary>
	/// 数据访问类:Users
	/// </summary>
	public partial class Users
	{
		public Users()
		{}
		#region  Method



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(DBHelper.Model.Users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Users(");
			strSql.Append("UserID,UserPassword,UserName,Sex,GraduateYear,GraduateCollege,Major,Education,RegAddress,CurAddress,Mobile,Tel,Email,QQ,Isemployed,InCity,CompanyType,CurCompanyName,unEmployedReason,Mark,Status,DocPath,Age,IDCard)");
			strSql.Append(" values (");
			strSql.Append("@UserID,@UserPassword,@UserName,@Sex,@GraduateYear,@GraduateCollege,@Major,@Education,@RegAddress,@CurAddress,@Mobile,@Tel,@Email,@QQ,@Isemployed,@InCity,@CompanyType,@CurCompanyName,@unEmployedReason,@Mark,@Status,@DocPath,@Age,@IDCard)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.NChar,10),
					new SqlParameter("@UserPassword", SqlDbType.NChar,20),
					new SqlParameter("@UserName", SqlDbType.NChar,30),
					new SqlParameter("@Sex", SqlDbType.NChar,2),
					new SqlParameter("@GraduateYear", SqlDbType.SmallInt,2),
					new SqlParameter("@GraduateCollege", SqlDbType.NChar,20),
					new SqlParameter("@Major", SqlDbType.NVarChar,50),
					new SqlParameter("@Education", SqlDbType.SmallInt,2),
					new SqlParameter("@RegAddress", SqlDbType.NChar,50),
					new SqlParameter("@CurAddress", SqlDbType.NChar,50),
					new SqlParameter("@Mobile", SqlDbType.Decimal,9),
					new SqlParameter("@Tel", SqlDbType.Decimal,9),
					new SqlParameter("@Email", SqlDbType.NChar,30),
					new SqlParameter("@QQ", SqlDbType.NChar,15),
					new SqlParameter("@Isemployed", SqlDbType.TinyInt,1),
					new SqlParameter("@InCity", SqlDbType.TinyInt,1),
					new SqlParameter("@CompanyType", SqlDbType.VarChar,50),
					new SqlParameter("@CurCompanyName", SqlDbType.NChar,30),
					new SqlParameter("@unEmployedReason", SqlDbType.NChar,30),
					new SqlParameter("@Mark", SqlDbType.NVarChar,200),
					new SqlParameter("@Status", SqlDbType.NChar,10),
					new SqlParameter("@DocPath", SqlDbType.NChar,30),
					new SqlParameter("@Age", SqlDbType.SmallInt,2),
					new SqlParameter("@IDCard", SqlDbType.NChar,20)};
			parameters[0].Value = model.UserID;
			parameters[1].Value = model.UserPassword;
			parameters[2].Value = model.UserName;
			parameters[3].Value = model.Sex;
			parameters[4].Value = model.GraduateYear;
			parameters[5].Value = model.GraduateCollege;
			parameters[6].Value = model.Major;
			parameters[7].Value = model.Education;
			parameters[8].Value = model.RegAddress;
			parameters[9].Value = model.CurAddress;
			parameters[10].Value = model.Mobile;
			parameters[11].Value = model.Tel;
			parameters[12].Value = model.Email;
			parameters[13].Value = model.QQ;
			parameters[14].Value = model.Isemployed;
			parameters[15].Value = model.InCity;
			parameters[16].Value = model.CompanyType;
			parameters[17].Value = model.CurCompanyName;
			parameters[18].Value = model.unEmployedReason;
			parameters[19].Value = model.Mark;
			parameters[20].Value = model.Status;
			parameters[21].Value = model.DocPath;
			parameters[22].Value = model.Age;
			parameters[23].Value = model.IDCard;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DBHelper.Model.Users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Users set ");
			strSql.Append("UserID=@UserID,");
			strSql.Append("UserPassword=@UserPassword,");
			strSql.Append("UserName=@UserName,");
			strSql.Append("Sex=@Sex,");
			strSql.Append("GraduateYear=@GraduateYear,");
			strSql.Append("GraduateCollege=@GraduateCollege,");
			strSql.Append("Major=@Major,");
			strSql.Append("Education=@Education,");
			strSql.Append("RegAddress=@RegAddress,");
			strSql.Append("CurAddress=@CurAddress,");
			strSql.Append("Mobile=@Mobile,");
			strSql.Append("Tel=@Tel,");
			strSql.Append("Email=@Email,");
			strSql.Append("QQ=@QQ,");
			strSql.Append("Isemployed=@Isemployed,");
			strSql.Append("InCity=@InCity,");
			strSql.Append("CompanyType=@CompanyType,");
			strSql.Append("CurCompanyName=@CurCompanyName,");
			strSql.Append("unEmployedReason=@unEmployedReason,");
			strSql.Append("Mark=@Mark,");
			strSql.Append("Status=@Status,");
			strSql.Append("DocPath=@DocPath,");
			strSql.Append("Age=@Age,");
			strSql.Append("IDCard=@IDCard");
			strSql.Append(" where UserCode=@UserCode");
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.NChar,10),
					new SqlParameter("@UserPassword", SqlDbType.NChar,20),
					new SqlParameter("@UserName", SqlDbType.NChar,30),
					new SqlParameter("@Sex", SqlDbType.NChar,2),
					new SqlParameter("@GraduateYear", SqlDbType.SmallInt,2),
					new SqlParameter("@GraduateCollege", SqlDbType.NChar,20),
					new SqlParameter("@Major", SqlDbType.NVarChar,50),
					new SqlParameter("@Education", SqlDbType.SmallInt,2),
					new SqlParameter("@RegAddress", SqlDbType.NChar,50),
					new SqlParameter("@CurAddress", SqlDbType.NChar,50),
					new SqlParameter("@Mobile", SqlDbType.Decimal,9),
					new SqlParameter("@Tel", SqlDbType.Decimal,9),
					new SqlParameter("@Email", SqlDbType.NChar,30),
					new SqlParameter("@QQ", SqlDbType.NChar,15),
					new SqlParameter("@Isemployed", SqlDbType.TinyInt,1),
					new SqlParameter("@InCity", SqlDbType.TinyInt,1),
					new SqlParameter("@CompanyType", SqlDbType.VarChar,50),
					new SqlParameter("@CurCompanyName", SqlDbType.NChar,30),
					new SqlParameter("@unEmployedReason", SqlDbType.NChar,30),
					new SqlParameter("@Mark", SqlDbType.NVarChar,200),
					new SqlParameter("@Status", SqlDbType.NChar,10),
					new SqlParameter("@DocPath", SqlDbType.NChar,30),
					new SqlParameter("@Age", SqlDbType.SmallInt,2),
					new SqlParameter("@IDCard", SqlDbType.NChar,20),
					new SqlParameter("@UserCode", SqlDbType.SmallInt,2)};
			parameters[0].Value = model.UserID;
			parameters[1].Value = model.UserPassword;
			parameters[2].Value = model.UserName;
			parameters[3].Value = model.Sex;
			parameters[4].Value = model.GraduateYear;
			parameters[5].Value = model.GraduateCollege;
			parameters[6].Value = model.Major;
			parameters[7].Value = model.Education;
			parameters[8].Value = model.RegAddress;
			parameters[9].Value = model.CurAddress;
			parameters[10].Value = model.Mobile;
			parameters[11].Value = model.Tel;
			parameters[12].Value = model.Email;
			parameters[13].Value = model.QQ;
			parameters[14].Value = model.Isemployed;
			parameters[15].Value = model.InCity;
			parameters[16].Value = model.CompanyType;
			parameters[17].Value = model.CurCompanyName;
			parameters[18].Value = model.unEmployedReason;
			parameters[19].Value = model.Mark;
			parameters[20].Value = model.Status;
			parameters[21].Value = model.DocPath;
			parameters[22].Value = model.Age;
			parameters[23].Value = model.IDCard;
			parameters[24].Value = model.UserCode;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int UserCode)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Users ");
			strSql.Append(" where UserCode=@UserCode");
			SqlParameter[] parameters = {
					new SqlParameter("@UserCode", SqlDbType.SmallInt)
			};
			parameters[0].Value = UserCode;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string UserCodelist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Users ");
			strSql.Append(" where UserCode in ("+UserCodelist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        public DBHelper.Model.Users GetModel(string UserID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 UserCode,UserID,UserPassword,UserName,Sex,GraduateYear,GraduateCollege,Major,Education,RegAddress,CurAddress,Mobile,Tel,Email,QQ,Isemployed,InCity,CompanyType,CurCompanyName,unEmployedReason,Mark,Status,DocPath,Age,IDCard from Users ");
            strSql.Append(" where UserID=@UserID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.NChar)
			};
            parameters[0].Value = UserID;

            SqlDataReader reader = DbHelperSQL.ExecuteReader(strSql.ToString(), parameters);

            if (reader.Read())
            {
                int UserCode = (int)reader[0];
                return GetModel(UserCode);
            }
            else
            {
                return null;
            }
        }
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DBHelper.Model.Users GetModel(int UserCode)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 UserCode,UserID,UserPassword,UserName,Sex,GraduateYear,GraduateCollege,Major,Education,RegAddress,CurAddress,Mobile,Tel,Email,QQ,Isemployed,InCity,CompanyType,CurCompanyName,unEmployedReason,Mark,Status,DocPath,Age,IDCard from Users ");
			strSql.Append(" where UserCode=@UserCode");
			SqlParameter[] parameters = {
					new SqlParameter("@UserCode", SqlDbType.SmallInt)
			};
			parameters[0].Value = UserCode;

			DBHelper.Model.Users model=new DBHelper.Model.Users();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["UserCode"]!=null && ds.Tables[0].Rows[0]["UserCode"].ToString()!="")
				{
					model.UserCode=int.Parse(ds.Tables[0].Rows[0]["UserCode"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UserID"]!=null && ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
					model.UserID=ds.Tables[0].Rows[0]["UserID"].ToString();
				}
				if(ds.Tables[0].Rows[0]["UserPassword"]!=null && ds.Tables[0].Rows[0]["UserPassword"].ToString()!="")
				{
					model.UserPassword=ds.Tables[0].Rows[0]["UserPassword"].ToString();
				}
				if(ds.Tables[0].Rows[0]["UserName"]!=null && ds.Tables[0].Rows[0]["UserName"].ToString()!="")
				{
					model.UserName=ds.Tables[0].Rows[0]["UserName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Sex"]!=null && ds.Tables[0].Rows[0]["Sex"].ToString()!="")
				{
					model.Sex=ds.Tables[0].Rows[0]["Sex"].ToString();
				}
				if(ds.Tables[0].Rows[0]["GraduateYear"]!=null && ds.Tables[0].Rows[0]["GraduateYear"].ToString()!="")
				{
					model.GraduateYear=int.Parse(ds.Tables[0].Rows[0]["GraduateYear"].ToString());
				}
				if(ds.Tables[0].Rows[0]["GraduateCollege"]!=null && ds.Tables[0].Rows[0]["GraduateCollege"].ToString()!="")
				{
					model.GraduateCollege=ds.Tables[0].Rows[0]["GraduateCollege"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Major"]!=null && ds.Tables[0].Rows[0]["Major"].ToString()!="")
				{
					model.Major=ds.Tables[0].Rows[0]["Major"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Education"]!=null && ds.Tables[0].Rows[0]["Education"].ToString()!="")
				{
					model.Education=int.Parse(ds.Tables[0].Rows[0]["Education"].ToString());
				}
				if(ds.Tables[0].Rows[0]["RegAddress"]!=null && ds.Tables[0].Rows[0]["RegAddress"].ToString()!="")
				{
					model.RegAddress=ds.Tables[0].Rows[0]["RegAddress"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CurAddress"]!=null && ds.Tables[0].Rows[0]["CurAddress"].ToString()!="")
				{
					model.CurAddress=ds.Tables[0].Rows[0]["CurAddress"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Mobile"]!=null && ds.Tables[0].Rows[0]["Mobile"].ToString()!="")
				{
					model.Mobile=decimal.Parse(ds.Tables[0].Rows[0]["Mobile"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Tel"]!=null && ds.Tables[0].Rows[0]["Tel"].ToString()!="")
				{
					model.Tel=decimal.Parse(ds.Tables[0].Rows[0]["Tel"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Email"]!=null && ds.Tables[0].Rows[0]["Email"].ToString()!="")
				{
					model.Email=ds.Tables[0].Rows[0]["Email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["QQ"]!=null && ds.Tables[0].Rows[0]["QQ"].ToString()!="")
				{
					model.QQ=ds.Tables[0].Rows[0]["QQ"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Isemployed"]!=null && ds.Tables[0].Rows[0]["Isemployed"].ToString()!="")
				{
					model.Isemployed=int.Parse(ds.Tables[0].Rows[0]["Isemployed"].ToString());
				}
				if(ds.Tables[0].Rows[0]["InCity"]!=null && ds.Tables[0].Rows[0]["InCity"].ToString()!="")
				{
					model.InCity=int.Parse(ds.Tables[0].Rows[0]["InCity"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CompanyType"]!=null && ds.Tables[0].Rows[0]["CompanyType"].ToString()!="")
				{
					model.CompanyType=ds.Tables[0].Rows[0]["CompanyType"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CurCompanyName"]!=null && ds.Tables[0].Rows[0]["CurCompanyName"].ToString()!="")
				{
					model.CurCompanyName=ds.Tables[0].Rows[0]["CurCompanyName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["unEmployedReason"]!=null && ds.Tables[0].Rows[0]["unEmployedReason"].ToString()!="")
				{
					model.unEmployedReason=ds.Tables[0].Rows[0]["unEmployedReason"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Mark"]!=null && ds.Tables[0].Rows[0]["Mark"].ToString()!="")
				{
					model.Mark=ds.Tables[0].Rows[0]["Mark"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Status"]!=null && ds.Tables[0].Rows[0]["Status"].ToString()!="")
				{
					model.Status=ds.Tables[0].Rows[0]["Status"].ToString();
				}
				if(ds.Tables[0].Rows[0]["DocPath"]!=null && ds.Tables[0].Rows[0]["DocPath"].ToString()!="")
				{
					model.DocPath=ds.Tables[0].Rows[0]["DocPath"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Age"]!=null && ds.Tables[0].Rows[0]["Age"].ToString()!="")
				{
					model.Age=int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IDCard"]!=null && ds.Tables[0].Rows[0]["IDCard"].ToString()!="")
				{
					model.IDCard=ds.Tables[0].Rows[0]["IDCard"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserCode,UserID,UserPassword,UserName,Sex,GraduateYear,GraduateCollege,Major,Education,RegAddress,CurAddress,Mobile,Tel,Email,QQ,Isemployed,InCity,CompanyType,CurCompanyName,unEmployedReason,Mark,Status,DocPath,Age,IDCard ");
			strSql.Append(" FROM Users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" UserCode,UserID,UserPassword,UserName,Sex,GraduateYear,GraduateCollege,Major,Education,RegAddress,CurAddress,Mobile,Tel,Email,QQ,Isemployed,InCity,CompanyType,CurCompanyName,unEmployedReason,Mark,Status,DocPath,Age,IDCard ");
			strSql.Append(" FROM Users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.UserCode desc");
			}
			strSql.Append(")AS Row, T.*  from Users T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Users";
			parameters[1].Value = "UserCode";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

